package com.simba.server.framedecoder.decoders;

import com.simba.common.decoder.Decoder;
import com.simba.common.log.LogUtil;
import com.simba.common.utils.FrameUtils;
import com.simba.common.utils.StringUtil;
import com.simba.server.commands.impl.web.WebSlotStatusCmd;
import com.simba.server.components.*;
import com.simba.server.components.data.SlotInfo;
import com.simba.server.components.data.SorterInfo;
import org.apache.log4j.Logger;
import org.jboss.netty.channel.Channel;

public class SlotPackDecoder implements Decoder
{
	private static final int SORTER_NUM_POSITION = 3;

	private static class SlotPackDecoderHolder
	{
		private static final SlotPackDecoder INSTANCE = new SlotPackDecoder();
	}

	public static final String NAME = "SlotPack";
	protected final static Logger logger = Logger.getLogger(SlotPackDecoder.class);
	private static final int SLOT_ID_POSITION = 6;
	private static final int SLOT_STATUS_POSITION=5;

	public static SlotPackDecoder getDecoderInstance()
	{
		return SlotPackDecoderHolder.INSTANCE;
	}

	private SlotPackDecoder()
	{

	}

	/**
	 * 01(发送方)02(接收方)07(命令字)00 01(线体号)00(00解锁01锁格打印02锁格)02(格口)OD OA
	 * @param channel
	 * @param message
	 */
	@Override
	public void doDecode(Channel channel, Object message)
	{
		try
		{
			ChannelsManager.getInstance().updateClient(channel);
			byte[] content = (byte[]) message;
			LogUtil.LogInfo(logger,"[SlotPackDecoder]"+ StringUtil.byteToHexString(content)+"格口是"+content[6]+"状态是"+content[5]);

			int sorterID = FrameUtils.getInstance().getUnsignedShortBigEndian(SORTER_NUM_POSITION, content);
			SorterInfo wheelSorterInfo = SorterManager.getInstance().getWheelSorterByID(String.valueOf(sorterID));
			if (wheelSorterInfo == null)
			{
				LogUtil.LogError(logger, "[WheelSlotPackDecoder] Sorter not found! Sorter ID:" + sorterID);
				return;
			}

			int slotIdValue = content[SLOT_ID_POSITION];
			String slotKey = String.valueOf(slotIdValue);

			SlotInfo slotInfo = SlotManager.getInstance().getSlotInfo(slotKey);
			if (slotInfo == null)
			{
				LogUtil.LogError(logger, "[WheelSlotPackDecoder] Slot not found! Slot ID:"+slotKey);
				return;
			}

			int slotStatusValue = content[SLOT_STATUS_POSITION];
			switch (slotStatusValue){
				case SortCommonDefines.SLOT_SOFT_LOCK_FLAG:
					slotInfo.setSlotType("3");
					LogUtil.LogInfo(logger,"收到plc来的锁格打印报文"+ StringUtil.byteToHexString(content)+"格口是"+content[6]+"包牌是"+slotInfo.getBoxInfo().getBoxCode());

					if (wheelSorterInfo.isPacket()){
						SortControllerManager.getInstance().onSlotPackMessage(slotInfo);
					}else {
						slotInfo.setLockStatus(SortCommonDefines.SLOT_SOFT_LOCK_FLAG);
						CommandExecutor.executorWebCommand(WebSlotStatusCmd.NAME,slotInfo,false);
					}
					break;
				case SortCommonDefines.SLOT_SOFT_UNLOCK_FLAG:
					slotInfo.setSlotType("3");
					if (wheelSorterInfo.isPacket()){
						SortControllerManager.getInstance().onSlotUnlockMessage(slotInfo);
						SlotManager.getInstance().sendSlotPackInfoToScreen(slotKey, 0, false);
					}else {
						slotInfo.setLockStatus(SortCommonDefines.SLOT_SOFT_UNLOCK_FLAG);
						CommandExecutor.executorWebCommand(WebSlotStatusCmd.NAME, slotInfo,false);
					}
					break;
				case 2:
					//特殊情况， 单击锁格触发
					slotInfo.setSlotType("3");
					slotInfo.setLockStatus(SortCommonDefines.SLOT_SOFT_LOCK_FLAG);
					CommandExecutor.executorWebCommand(WebSlotStatusCmd.NAME,slotInfo,false);
					break;
				default:
					LogUtil.LogError(logger, "[WheelSlotPackDecoder] Unknown slot status! Slot status:"+slotStatusValue);
					return;
			}

			this.sendSlotPackToPLC(sorterID, slotIdValue, slotStatusValue);
		}
		catch (Exception ex)
		{
			LogUtil.LogError(SlotPackDecoder.logger,
					"Slot Pack Error -- Exception : " + ex.getMessage());
		}
	}

	@Override
	public String getName()
	{
		return SlotPackDecoder.NAME;
	}

	private void sendSlotPackToPLC(int sorterId, int slotId, int slotStatus)
	{
		SortProtocolCreator.getInstance().createSlotPackReply(sorterId, slotId, slotStatus);
	}
}
